package Linkedlist;

import java.sql.SQLOutput;

//单项链表
class Node {
    public String value;
    public Node zhi;


    public Node(String value) {
        this.value=value;
        this.zhi=null;
    }
}
public class MyLinkedList {
    private Node head = null;
   // private Node node=null;
    public int size (){
        Node TA=head;
        int size=0;
        for(;TA!=null;TA=TA.zhi){
            size++;
        }
        return size;
    }
    public void remove2(String value){
        Node cur=head;
        for(;cur!=null;cur=cur.zhi){
            if(cur.zhi.value.equals(value)){
                break;
            }
        }
        if(cur==null){
            return ;
        }
        Node remove=cur.zhi;
        cur.zhi=remove.zhi;
    }
public void remove(int index){
//    for(Node cur=head;cur!=null;cur=cur.zhi){
//        if(cur.value.equals(value)){
////                Node remove=cur;
//            //cur=cur.zhi;
//
//        }
//    }
    Node cur=head;
    for(int i=0;i<index-1;i++){
        cur=cur.zhi;
    }
    Node TO=cur.zhi;
    cur.zhi=TO.zhi;
}
    public int indexOf(String value){
        int a=0;
        for(Node cur =head;cur!=null;cur=cur.zhi){
            if(cur.value.equals(value))
                return a;
            a++;
        }
        return -1;
    }
    //判定某个元素是否在链表中包含
    public boolean contains(String value){
        //MyLinkedList list=new MyLinkedList();
        for(Node cur=head;cur!=null;cur=cur.zhi){
            if(cur.value.equals(value)){
                return true;
            }
        }
        return false;
    }
    public void add(int index,String value) {

        if(index<0||index>size()){
//            try{throw new Exception("下标超出范围");}
//            catch(Exception)
//            }
            System.out.println("下标超出范围");
        }
        int a=0;
        Node tail2=head;
        Node node=new Node(value);
//        for(;tail2!=null;tail2=tail2.zhi){
            if(index==0){
                addFirst(value);
                return;
            }
//            Node tail3=tail2;
//            a++;
//            if(index==a) {
//                tail3.zhi=node;
//                tail2=tail2.zhi;
//                node=tail2;
//                return;
//            }
//        }
        for(;a<index-1;a++){
            tail2=tail2.zhi;
        }
        node.zhi=tail2.zhi;
        tail2=node;
    }
    public void addFirst(String value) {
        Node node = new Node(value);
        node.zhi = head;
        head = node;
    }
    public void addLast(String value) {
        if(head==null){
            Node node =new Node(value);
            head.zhi=node;
            node.zhi=null;
            return;
        }
        //Mylinkedlist link=new Mylinkedlist();
        //Node node =new Node(value);
        Node tail=head;
        for(;tail!=null;tail=tail.zhi){
        //if(tail==null){
            //tail.zhi=
            //node.zhi= tail;
            //tail
            break;
    }
        Node node =new Node(value);
        tail.zhi=node;
        node.zhi=null;
    }
    public  String toString(){
        Node tail1=head;
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("[");
        for(;tail1!=null;tail1=tail1.zhi){
            stringBuilder.append(tail1.value);
            if(tail1.zhi!=null)
            stringBuilder.append(",");
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
    public static void test(){
        MyLinkedList list=new MyLinkedList();
        list.addFirst("a");
        list.addFirst("b");
        list.addFirst("c");
        list.addFirst("d");
        list.addFirst("e");
        list.add(0,"f");
        list.toString();
        System.out.println(list);
    }
    public static void test2(){
        MyLinkedList list =new MyLinkedList();
        list.addFirst("a");
        list.addFirst("b");
        list.addFirst("c");
        list.addFirst("d");
        list.addFirst("e");
        //list.remove(1);
        list.remove2("a");
//        System.out.println(list.contains("a"));
//        System.out.println(list.indexOf("b"));
        //System.out.println(list.remove(1));
        System.out.println(list);
    }
    public static void main(String[] args) {
        //test2();
    }
}
